package cn.yitun.chat.yitunserverimcore.general.main.push.video;

import cn.yitun.chat.yituncommonmessage.impl.HeadImpl;
import cn.yitun.chat.yituncommonmessage.push.PushMessage;
import cn.yitun.chat.yitunservercommonlib.constant.ActionTypeConstant;
import cn.yitun.chat.yitunservercommonlib.enums.control.push.video.VideoPushMethodEnum;
import cn.yitun.chat.yitunserverimcore.bridge.net.WriteHandler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class VideoPush {
	//1.5.002
	final String action = ActionTypeConstant.VIDEO_ACTION_PUSH;

	@Resource
	WriteHandler writeHandler;

	/**
	 * 服务器收到客户端视频后
	 * 
	 * @param userId
	 */
	public void pushReceivedVideoAddress(Long userId) {
		HeadImpl head = new HeadImpl();
		head.setAction(action);
		head.setMethod(VideoPushMethodEnum.RECEIVED_VIDEO_ADDRESS.getMethod());
		head.setTimestamp(System.currentTimeMillis());
		PushMessage message = new PushMessage();
		message.setHead(head);
		writeHandler.push(userId, message);
	}

	/**
	 * 请求
	 * @param key
	 * @param receiveUserId
	 * @param sendUserId
	 */
	public void pushRequestVideo(Long key,Long receiveUserId,Long sendUserId) {
		HeadImpl head = new HeadImpl();
		head.setAction(action);
		head.setMethod(VideoPushMethodEnum.REQUEST.getMethod());
		head.setTimestamp(System.currentTimeMillis());
		head.setKey(key);
		
		PushMessage message = new PushMessage();
		message.setHead(head);
		message.bodyPut("receiveUserId", receiveUserId);
		message.bodyPut("sendUserId", sendUserId);
		writeHandler.push(receiveUserId, message);
	}

	/**
	 * 响应
	 * @param key
	 * @param sendUserId
	 * @param receiveUserId
	 * @param actionType
	 */
	public void pushResponseVideo(Long key,Long sendUserId, Long receiveUserId,String actionType) {
		HeadImpl head = new HeadImpl();
		head.setAction(action);
		head.setMethod(VideoPushMethodEnum.RESPONSE.getMethod());
		head.setTimestamp(System.currentTimeMillis());
		head.setKey(key);
		
		PushMessage message = new PushMessage();
		message.setHead(head);
		message.bodyPut("receiveUserId", receiveUserId);
		message.bodyPut("sendUserId", sendUserId);
		message.bodyPut("actionType", actionType);
		writeHandler.push(receiveUserId, message);
	}
}
